package com.baseframe.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.blog.entity.SnsBlog;
import com.baseframe.blog.entity.request.SnsBlogRequest;
import com.baseframe.blog.entity.vo.SnsBlogVO;
import com.baseframe.blog.service.SnsBlogService;
import com.baseframe.blog.mapper.SnsBlogMapper;
import com.baseframe.blog.sign.BlogStatus;
import com.baseframe.mybatis.utils.PageUtil;
import com.baseframe.tool.result.R;
import com.baseframe.tool.utils.reptile.ReptileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**

* @description 针对表【sns_blog(博客表)】的数据库操作Service实现
* @createDate 2023-06-23 17:55:58
*/
@Service
public class SnsBlogServiceImpl extends ServiceImpl<SnsBlogMapper, SnsBlog>
    implements SnsBlogService{

    @Override
    public Page<SnsBlogVO> blogPageList(Page<SnsBlogVO> page, SnsBlogRequest request) {
        page = PageUtil.selectCountPage(page);
        Page<SnsBlogVO> resultPage = this.getBaseMapper().blogPageList(page,request);
        for (SnsBlogVO record : resultPage.getRecords()) {
            //设置博文状态
            record.setStatusName(BlogStatus.getNameByCode(record.getStatus()));
        }
        return resultPage;
    }

    @Override
    public boolean examineBlog(SnsBlogRequest request) {

        return this.update(new LambdaUpdateWrapper<SnsBlog>()
                .eq(SnsBlog::getId,request.getId())
                .set(SnsBlog::getStatus,request.getStatus())
                .set(BlogStatus.PUBLISHED == BlogStatus.getStatusByCode(request.getStatus()),SnsBlog::getOpinion,null)
                .set(BlogStatus.REVIEW_FAILED == BlogStatus.getStatusByCode(request.getStatus()),SnsBlog::getOpinion,request.getOpinion())
        );
    }

    @Override
    public List<SnsBlogVO> getNewList() {
        List<SnsBlogVO> newList = this.getBaseMapper().getNewList();

        for (SnsBlogVO snsBlogVO : newList) {

            if (snsBlogVO.getContent()!=null){
                //获取封面
                String firstImage = ReptileUtil.getFirstImage(snsBlogVO.getContent());
                snsBlogVO.setCover(firstImage);
            }

        }

        return newList;
    }

    @Override
    public Page<SnsBlogVO> blogPageListByFront(Page<SnsBlogVO> page, SnsBlogRequest request) {
        page = PageUtil.selectCountPage(page);
        Page<SnsBlogVO> resultPage = this.getBaseMapper().blogPageListByFront(page,request);
        for (SnsBlogVO record : resultPage.getRecords()) {
            String firstImage = ReptileUtil.getFirstImage(record.getContent());
            record.setCover(firstImage);
        }
        return resultPage;
    }

    @Override
    public SnsBlogVO getDetails(SnsBlogRequest request) {

        return this.getBaseMapper().getDetails(request);
    }

    @Override
    public Page<SnsBlogVO> ownBlogPageList(Page<SnsBlogVO> page, SnsBlogRequest request) {

        page = PageUtil.selectCountPage(page);

        Page<SnsBlogVO> resultPage = this.getBaseMapper().ownBlogPageList(page, request, AuthUtil.getUserId());
        for (SnsBlogVO record : resultPage.getRecords()) {
            String firstImage = ReptileUtil.getFirstImage(record.getContent());
            record.setCover(firstImage);
            record.setStatusName(BlogStatus.getNameByCode(record.getStatus()));
        }
        return resultPage;
    }

    @Override
    public SnsBlogVO getDetailsByUnconditional(SnsBlogRequest request) {

        SnsBlog byId = getById(request.getId());
        SnsBlogVO blogVO = new SnsBlogVO();
        BeanUtils.copyProperties(byId,blogVO);

        return blogVO;
    }
}




